package org.dromara.bean.service.impl;

import org.dromara.bean.domain.vo.HisQuestionnaireVo;
import org.dromara.bean.mapper.HisQuestionnaireMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.bean.domain.bo.HisQuestionnaireAnswerBo;
import org.dromara.bean.domain.vo.HisQuestionnaireAnswerVo;
import org.dromara.bean.domain.HisQuestionnaireAnswer;
import org.dromara.bean.mapper.HisQuestionnaireAnswerMapper;
import org.dromara.bean.service.IHisQuestionnaireAnswerService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 答题记录Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-02
 */
@RequiredArgsConstructor
@Service
public class HisQuestionnaireAnswerServiceImpl implements IHisQuestionnaireAnswerService {

    private final HisQuestionnaireAnswerMapper baseMapper;

    private final HisQuestionnaireMapper questionnaireMapper;

    /**
     * 查询答题记录
     *
     * @param id 主键
     * @return 答题记录
     */
    @Override
    public HisQuestionnaireAnswerVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询答题记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 答题记录分页列表
     */
    @Override
    public TableDataInfo<HisQuestionnaireAnswerVo> queryPageList(HisQuestionnaireAnswerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisQuestionnaireAnswer> lqw = buildQueryWrapper(bo);
        Page<HisQuestionnaireAnswerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(r -> {
            r.setQuestionnaireVo(questionnaireMapper.selectVoById(r.getQuestionnaireId()));
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的答题记录列表
     *
     * @param bo 查询条件
     * @return 答题记录列表
     */
    @Override
    public List<HisQuestionnaireAnswerVo> queryList(HisQuestionnaireAnswerBo bo) {
        LambdaQueryWrapper<HisQuestionnaireAnswer> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HisQuestionnaireAnswer> buildQueryWrapper(HisQuestionnaireAnswerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisQuestionnaireAnswer> lqw = Wrappers.lambdaQuery();
        lqw.eq( HisQuestionnaireAnswer::getMemberId, LoginHelper.getUserId());
        lqw.eq(bo.getQuestionnaireId() != null, HisQuestionnaireAnswer::getQuestionnaireId, bo.getQuestionnaireId());
        lqw.eq(StringUtils.isNotBlank(bo.getAnswer()), HisQuestionnaireAnswer::getAnswer, bo.getAnswer());
        return lqw;
    }

    /**
     * 新增答题记录
     *
     * @param bo 答题记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HisQuestionnaireAnswerBo bo) {
        HisQuestionnaireAnswer add = MapstructUtils.convert(bo, HisQuestionnaireAnswer.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改答题记录
     *
     * @param bo 答题记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HisQuestionnaireAnswerBo bo) {
        HisQuestionnaireAnswer update = MapstructUtils.convert(bo, HisQuestionnaireAnswer.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HisQuestionnaireAnswer entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除答题记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
